#!/bin/bash

# TMS模块重构脚本 - 阶段1: 只重构代码,保持数据库表名不变
# 执行前请确保代码已提交到git

set -e

echo "=========================================="
echo "TMS模块重构 - 阶段1: 代码重构"
echo "ProductInfo -> Order (保持数据库表名)"
echo "=========================================="
echo ""

BASE_PATH="/Users/glenn/github/ruoyi-vue-pro/yudao-module-tms"
cd "$BASE_PATH"

echo "当前目录: $(pwd)"
echo ""

# 检查是否有未提交的更改
if ! git diff-index --quiet HEAD --; then
    echo "警告: 检测到未提交的更改"
    echo "建议先提交或暂存当前更改"
    read -p "是否继续? (y/n) " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        exit 1
    fi
fi

echo "步骤 1: 重命名目录..."

# Controller
if [ -d "src/main/java/cn/iocoder/yudao/module/tms/controller/admin/productinfo" ]; then
    echo "  - controller/admin/productinfo -> order"
    git mv src/main/java/cn/iocoder/yudao/module/tms/controller/admin/productinfo \
           src/main/java/cn/iocoder/yudao/module/tms/controller/admin/order
fi

# DataObject
if [ -d "src/main/java/cn/iocoder/yudao/module/tms/dal/dataobject/productinfo" ]; then
    echo "  - dal/dataobject/productinfo -> order"
    git mv src/main/java/cn/iocoder/yudao/module/tms/dal/dataobject/productinfo \
           src/main/java/cn/iocoder/yudao/module/tms/dal/dataobject/order
fi

# Mapper
if [ -d "src/main/java/cn/iocoder/yudao/module/tms/dal/mysql/productinfo" ]; then
    echo "  - dal/mysql/productinfo -> order"
    git mv src/main/java/cn/iocoder/yudao/module/tms/dal/mysql/productinfo \
           src/main/java/cn/iocoder/yudao/module/tms/dal/mysql/order
fi

# Service
if [ -d "src/main/java/cn/iocoder/yudao/module/tms/service/productinfo" ]; then
    echo "  - service/productinfo -> order"
    git mv src/main/java/cn/iocoder/yudao/module/tms/service/productinfo \
           src/main/java/cn/iocoder/yudao/module/tms/service/order
fi

# Mapper XML
if [ -d "src/main/resources/mapper/productinfo" ]; then
    echo "  - mapper/productinfo -> order"
    git mv src/main/resources/mapper/productinfo \
           src/main/resources/mapper/order
fi

echo ""
echo "步骤 2: 重命名 Java 文件..."

cd "$BASE_PATH"

# Controller
[ -f "src/main/java/cn/iocoder/yudao/module/tms/controller/admin/order/ProductInfoController.java" ] && \
    git mv src/main/java/cn/iocoder/yudao/module/tms/controller/admin/order/ProductInfoController.java \
           src/main/java/cn/iocoder/yudao/module/tms/controller/admin/order/OrderController.java && \
    echo "  - ProductInfoController.java -> OrderController.java"

# VO files
[ -f "src/main/java/cn/iocoder/yudao/module/tms/controller/admin/order/vo/ProductInfoPageReqVO.java" ] && \
    git mv src/main/java/cn/iocoder/yudao/module/tms/controller/admin/order/vo/ProductInfoPageReqVO.java \
           src/main/java/cn/iocoder/yudao/module/tms/controller/admin/order/vo/OrderPageReqVO.java && \
    echo "  - ProductInfoPageReqVO.java -> OrderPageReqVO.java"

[ -f "src/main/java/cn/iocoder/yudao/module/tms/controller/admin/order/vo/ProductInfoRespVO.java" ] && \
    git mv src/main/java/cn/iocoder/yudao/module/tms/controller/admin/order/vo/ProductInfoRespVO.java \
           src/main/java/cn/iocoder/yudao/module/tms/controller/admin/order/vo/OrderRespVO.java && \
    echo "  - ProductInfoRespVO.java -> OrderRespVO.java"

[ -f "src/main/java/cn/iocoder/yudao/module/tms/controller/admin/order/vo/ProductInfoSaveReqVO.java" ] && \
    git mv src/main/java/cn/iocoder/yudao/module/tms/controller/admin/order/vo/ProductInfoSaveReqVO.java \
           src/main/java/cn/iocoder/yudao/module/tms/controller/admin/order/vo/OrderSaveReqVO.java && \
    echo "  - ProductInfoSaveReqVO.java -> OrderSaveReqVO.java"

# DO files
[ -f "src/main/java/cn/iocoder/yudao/module/tms/dal/dataobject/order/ProductInfoDO.java" ] && \
    git mv src/main/java/cn/iocoder/yudao/module/tms/dal/dataobject/order/ProductInfoDO.java \
           src/main/java/cn/iocoder/yudao/module/tms/dal/dataobject/order/OrderDO.java && \
    echo "  - ProductInfoDO.java -> OrderDO.java"

[ -f "src/main/java/cn/iocoder/yudao/module/tms/dal/dataobject/order/ProductInfoRefImageDO.java" ] && \
    git mv src/main/java/cn/iocoder/yudao/module/tms/dal/dataobject/order/ProductInfoRefImageDO.java \
           src/main/java/cn/iocoder/yudao/module/tms/dal/dataobject/order/OrderRefImageDO.java && \
    echo "  - ProductInfoRefImageDO.java -> OrderRefImageDO.java"

# Mapper files
[ -f "src/main/java/cn/iocoder/yudao/module/tms/dal/mysql/order/ProductInfoMapper.java" ] && \
    git mv src/main/java/cn/iocoder/yudao/module/tms/dal/mysql/order/ProductInfoMapper.java \
           src/main/java/cn/iocoder/yudao/module/tms/dal/mysql/order/OrderMapper.java && \
    echo "  - ProductInfoMapper.java -> OrderMapper.java"

[ -f "src/main/java/cn/iocoder/yudao/module/tms/dal/mysql/order/ProductInfoRefImageMapper.java" ] && \
    git mv src/main/java/cn/iocoder/yudao/module/tms/dal/mysql/order/ProductInfoRefImageMapper.java \
           src/main/java/cn/iocoder/yudao/module/tms/dal/mysql/order/OrderRefImageMapper.java && \
    echo "  - ProductInfoRefImageMapper.java -> OrderRefImageMapper.java"

# Service files
[ -f "src/main/java/cn/iocoder/yudao/module/tms/service/order/ProductInfoService.java" ] && \
    git mv src/main/java/cn/iocoder/yudao/module/tms/service/order/ProductInfoService.java \
           src/main/java/cn/iocoder/yudao/module/tms/service/order/OrderService.java && \
    echo "  - ProductInfoService.java -> OrderService.java"

[ -f "src/main/java/cn/iocoder/yudao/module/tms/service/order/ProductInfoServiceImpl.java" ] && \
    git mv src/main/java/cn/iocoder/yudao/module/tms/service/order/ProductInfoServiceImpl.java \
           src/main/java/cn/iocoder/yudao/module/tms/service/order/OrderServiceImpl.java && \
    echo "  - ProductInfoServiceImpl.java -> OrderServiceImpl.java"

[ -f "src/main/java/cn/iocoder/yudao/module/tms/service/order/ProductInfoRefImageService.java" ] && \
    git mv src/main/java/cn/iocoder/yudao/module/tms/service/order/ProductInfoRefImageService.java \
           src/main/java/cn/iocoder/yudao/module/tms/service/order/OrderRefImageService.java && \
    echo "  - ProductInfoRefImageService.java -> OrderRefImageService.java"

[ -f "src/main/java/cn/iocoder/yudao/module/tms/service/order/ProductInfoRefImageServiceImpl.java" ] && \
    git mv src/main/java/cn/iocoder/yudao/module/tms/service/order/ProductInfoRefImageServiceImpl.java \
           src/main/java/cn/iocoder/yudao/module/tms/service/order/OrderRefImageServiceImpl.java && \
    echo "  - ProductInfoRefImageServiceImpl.java -> OrderRefImageServiceImpl.java"

# Mapper XML files
[ -f "src/main/resources/mapper/order/ProductInfoMapper.xml" ] && \
    git mv src/main/resources/mapper/order/ProductInfoMapper.xml \
           src/main/resources/mapper/order/OrderMapper.xml && \
    echo "  - ProductInfoMapper.xml -> OrderMapper.xml"

[ -f "src/main/resources/mapper/order/ProductInfoRefImageMapper.xml" ] && \
    git mv src/main/resources/mapper/order/ProductInfoRefImageMapper.xml \
           src/main/resources/mapper/order/OrderRefImageMapper.xml && \
    echo "  - ProductInfoRefImageMapper.xml -> OrderRefImageMapper.xml"

echo ""
echo "步骤 3: 替换文件内容 (包名、类名、变量名)..."
echo "  注意: 保持数据库表名 tms_product_info 不变"

# 替换 Java 文件中的内容
# 注意: 不替换 @TableName 注解中的表名
find src/main/java -type f -name "*.java" -print0 | while IFS= read -r -d '' file; do
    # 替换包名
    sed -i '' 's/\.productinfo\./\.order\./g' "$file"
    sed -i '' 's/\/productinfo\//\/order\//g' "$file"
    
    # 替换类名 (但不替换注释中的表名)
    sed -i '' 's/ProductInfoDO/OrderDO/g' "$file"
    sed -i '' 's/ProductInfoMapper/OrderMapper/g' "$file"
    sed -i '' 's/ProductInfoService/OrderService/g' "$file"
    sed -i '' 's/ProductInfoController/OrderController/g' "$file"
    sed -i '' 's/ProductInfoPageReqVO/OrderPageReqVO/g' "$file"
    sed -i '' 's/ProductInfoRespVO/OrderRespVO/g' "$file"
    sed -i '' 's/ProductInfoSaveReqVO/OrderSaveReqVO/g' "$file"
    sed -i '' 's/ProductInfoRefImageDO/OrderRefImageDO/g' "$file"
    sed -i '' 's/ProductInfoRefImageMapper/OrderRefImageMapper/g' "$file"
    sed -i '' 's/ProductInfoRefImageService/OrderRefImageService/g' "$file"
    
    # 替换变量名
    sed -i '' 's/productInfo/order/g' "$file"
    sed -i '' 's/ProductInfo/Order/g' "$file"
done

echo "  - Java 文件内容已更新"

# 替换 XML 文件中的内容
find src/main/resources/mapper/order -type f -name "*.xml" -print0 | while IFS= read -r -d '' file; do
    # 替换 namespace 和类引用
    sed -i '' 's/\.productinfo\./\.order\./g' "$file"
    sed -i '' 's/ProductInfoDO/OrderDO/g' "$file"
    sed -i '' 's/ProductInfoMapper/OrderMapper/g' "$file"
    sed -i '' 's/ProductInfoRefImageDO/OrderRefImageDO/g' "$file"
    sed -i '' 's/ProductInfoRefImageMapper/OrderRefImageMapper/g' "$file"
done

echo "  - Mapper XML 文件已更新"

# 不修改统计相关的 XML,因为它们使用实际的数据库表名
echo "  - 统计 Mapper 保持使用 tms_product_info 表名"

echo ""
echo "步骤 4: 清理编译文件..."
if [ -d "target" ]; then
    rm -rf target
    echo "  - target 目录已清理"
fi

echo ""
echo "=========================================="
echo "阶段1 重构完成!"
echo "=========================================="
echo ""
echo "变更内容:"
echo "  ✓ 包名: productinfo -> order"
echo "  ✓ 类名: ProductInfo* -> Order*"
echo "  ✓ 变量名: productInfo -> order"
echo "  ✓ 文件名: ProductInfo* -> Order*"
echo "  ✓ 目录名: productinfo -> order"
echo "  ✗ 数据库表名: 保持 tms_product_info (未修改)"
echo ""
echo "下一步操作:"
echo "1. 查看变更: git status"
echo "2. 查看差异: git diff"
echo "3. 编译验证: cd .. && mvn clean compile -pl yudao-module-tms"
echo "4. 如果有问题,回滚: git reset --hard HEAD"
echo "5. 提交变更: git add . && git commit -m 'refactor(tms): rename productInfo to order in code'"
echo ""
echo "可选: 执行 refactor_step2_database.sql 来重命名数据库表"
echo ""
